import 'package:flutter_admin/pages/common/cry/cry_all.dart';
import 'package:flutter_admin/pages/common/cry/vo/select_option_vo.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_admin/pages/common/cry/cry_button.dart';
import 'package:flutter_admin/api/odoo/dynamic_data_api.dart';
import 'package:flutter_admin/constants/constant.dart';
import 'package:flutter_admin/models/odoo/udt_tb_workreportrecord.dart';
import 'package:flutter_admin/pages/common/date_time_select.dart';
import 'package:flutter_admin/utils/store_util.dart';
import 'package:flutter_admin/utils/utils.dart';
import 'package:intl/intl.dart';
import 'package:quiver/strings.dart';
import '../../../generated/l10n.dart';

class UdtTbWorkreportrecordDialog extends StatefulWidget {
  UdtTbWorkreportrecordDialog();

  @override
  _UdtTbWorkreportrecordDialogState createState() =>
      _UdtTbWorkreportrecordDialogState();
}

class _UdtTbWorkreportrecordDialogState
    extends State<UdtTbWorkreportrecordDialog> {
  // late List<Widget> children;
  final GlobalKey<FormState> formKey = GlobalKey<FormState>();

  UdtTbWorkreportrecord udtTbWorkreportrecord = UdtTbWorkreportrecord();

  //工厂
  List<SelectOptionVO> factoryList = [];

  //车间
  List<SelectOptionVO> workshopList = [];

  //工位
  List<SelectOptionVO> stationList = [];

  //是否确认
  List<SelectOptionVO> statusList = [];

  DateFormat format = DateFormat('yyyy-MM-dd HH:mm:ss');

  @override
  void initState() {
    super.initState();
    _loadData();
  }

  _loadData() async {
    if (StoreUtil.hasData(Constant.KEY_PRODUCTIONSTATION)) {
      udtTbWorkreportrecord.stationName =
          StoreUtil.read(Constant.KEY_PRODUCTIONSTATION)["stationName"];
    }
    udtTbWorkreportrecord.reportDateStart = Utils.getDate()+" 00:00:00";
    udtTbWorkreportrecord.reportDateEnd = Utils.getDate()+" 23:59:59";
    factoryList = await DynamicDataApi.getModelSelect(
        model: "hr.department",
        name: "complete_name",
        key: "parent_id",
        operator: "=",
        val: null);
    statusList = [
      SelectOptionVO(value: "", label: ""),
      SelectOptionVO(value: "0", label: "未确认"),
      SelectOptionVO(value: "1", label: "已确认"),
    ];
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    var form = Form(
      key: formKey,
      child: Wrap(
        children: <Widget>[
          // CrySelect(
          //   label: '生产工厂',
          //   dataList: factoryList,
          //   value: udtTbWorkreportrecord.factoryId,
          //   onSaved: (v) {
          //     udtTbWorkreportrecord.factoryId = v;
          //   },
          //   onChange: (v) async {
          //     if (udtTbWorkreportrecord.factoryId != v) {
          //       formKey.currentState!.save();
          //       udtTbWorkreportrecord.factoryId = v;
          //       udtTbWorkreportrecord.workshopId = null;
          //       udtTbWorkreportrecord.stationId = null;
          //       stationList = [];
          //       try {
          //         workshopList = await DynamicDataApi.getModelSelect(
          //             model: "hr.department",
          //             name: "complete_name",
          //             key: "parent_id",
          //             operator: "=",
          //             val: int.parse(v));
          //       } catch (e) {
          //         workshopList = [];
          //       }
          //       setState(() {});
          //     }
          //   },
          // ),
          // CrySelect(
          //   label: '生产车间',
          //   dataList: workshopList,
          //   value: udtTbWorkreportrecord.workshopId,
          //   onSaved: (v) {
          //     udtTbWorkreportrecord.workshopId = v;
          //   },
          //   onChange: (v) async {
          //     if (udtTbWorkreportrecord.workshopId != v) {
          //       formKey.currentState!.save();
          //       udtTbWorkreportrecord.workshopId = v;
          //       udtTbWorkreportrecord.stationId = null;
          //       try {
          //         stationList = await DynamicDataApi.getModelSelect(
          //             model: "base.productionstation",
          //             name: "stationname",
          //             key: 'workshop_id',
          //             val: int.parse(v));
          //       } catch (e) {
          //         stationList = [];
          //       }
          //       setState(() {});
          //     }
          //   },
          // ),
          // CrySelect(
          //   label: '设备工位',
          //   dataList: stationList,
          //   value: udtTbWorkreportrecord.stationId,
          //   onSaved: (v) {
          //     udtTbWorkreportrecord.stationId = v;
          //   },
          // ),
          CryInput(
            label: '设备工位',
            value: udtTbWorkreportrecord.stationName,
            enable: false,
            onSaved: (v) {
              udtTbWorkreportrecord.stationName = v;
            },
          ),
          CryInput(
            label: '产品名称',
            value: udtTbWorkreportrecord.productName,
            onSaved: (v) {
              udtTbWorkreportrecord.productName = v;
            },
          ),
          CryInput(
            label: '生产单号',
            value: udtTbWorkreportrecord.productionorderno,
            onSaved: (v) {
              udtTbWorkreportrecord.productionorderno = v;
            },
          ),
          CrySelect(
            dataList: statusList,
            label: '是否确认',
            value: udtTbWorkreportrecord.status,
            onSaved: (v) {
              udtTbWorkreportrecord.status = v;
            },
          ),
          DateTimeSelect(
            context,
            label: '报工开始时间',
            value: udtTbWorkreportrecord.reportDateStart,
            lastDate: isNotBlank(udtTbWorkreportrecord.reportDateEnd)
                ? format.parse(udtTbWorkreportrecord.reportDateEnd!)
                : null,
            onDateChange: (v) {
              formKey.currentState?.save();
              udtTbWorkreportrecord.reportDateStart = v;
              setState(() {});
            },
            onSaved: (v) {
              udtTbWorkreportrecord.reportDateStart = v;
            },
          ),
          DateTimeSelect(
            context,
            label: '报工结束时间',
            value: udtTbWorkreportrecord.reportDateEnd,
            firstDate: isNotBlank(udtTbWorkreportrecord.reportDateStart)
                ? format.parse(udtTbWorkreportrecord.reportDateStart!)
                : null,
            onDateChange: (v) {
              formKey.currentState?.save();
              udtTbWorkreportrecord.reportDateEnd = v;
              setState(() {});
            },
            onSaved: (v) {
              udtTbWorkreportrecord.reportDateEnd = v;
            },
          ),
        ],
      ),
    );
    var buttonBar = ButtonBar(
      alignment: MainAxisAlignment.center,
      children: <Widget>[
        CryButton(
          label: "确定",
          onPressed: () {
            FormState form = formKey.currentState!;
            form.save();
            Navigator.pop(context, udtTbWorkreportrecord);
          },
          iconData: Icons.save,
        ),
        CryButton(
          label: S.of(context).cancel,
          onPressed: () {
            Navigator.pop(context);
          },
          iconData: Icons.cancel,
        )
      ],
    );
    var result = Scaffold(
      appBar: AppBar(
        title: Text("查询"),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [form],
        ),
      ),
      bottomNavigationBar: buttonBar,
    );
    return SizedBox(
      width: 650,
      height: 650,
      child: result,
    );
    // return result;
  }
}
